var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../../common/common');
var DBConfig = require('../../../db/dbconfig');
var Wx_config = require('../../../wx_config/wx_config');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);
	

// 订单 评价
router.post('/jkyx/order_comment', function(req, res, next) {
	var param = req.body || ''; 
	// 用户名
	var account_id = param.account_id || '';
	// 订单ID
	var order_id = param.order_detail.id || '';
	// 时间
	var create_time = CommonJS.timestamp();
	if (param == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'101',
 		}));
 		return;
	}
	// 1.修改订单评价字段为已评价状态 2.插入评价数据
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		// 处理评价
		var comments = param.order_detail.goods;
		var comments_len = comments.length;
		var values2 = '';
		for (var i = 0; i < comments_len; i ++) {
			var goods_id = comments[i].id;
			var content = comments[i].comment;
			var images = comments[i].upLoads2 || '';
			var reply = '';
			var advice = '';
			var status = 1;
			var score_goods = comments[i].starts.score;
			var values = "'" + account_id + "'" + ',' 
  	  			 + "'" + order_id + "'" + ',' 
  	  			 + "'" + goods_id + "'" + ',' 
  	  			 + "'" + content + "'" + ','
  	  			 + "'" + images + "'" + ','
  	  			 + "'" + score_goods + "'" + ',' 
  	  			 + "'" + reply + "'" + ','
  	             + "'" + advice + "'" + ',' 
  	             + "'" + status + "'" + ',' 
  	             + "'" + create_time + "'";
  	        if (i == (comments_len - 1)) {
  	        		values = '(' + values + ')';
  	        }else{
  	        		values = '(' + values + '),';
  	        }
  	        values2 = values2 + values;
		}
		var inserSql = 'insert into comment (account_id,order_id,goods_id,content,images,score_goods,reply,advice,status,create_time) values ' + values2 + ';'; 
		var order_comment_sql = DBConfig.updateOrderComment + inserSql;
//		console.log(order_comment_sql);
		connection.query(order_comment_sql,[order_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100'
	     		}));
			}
		})
	})
});


module.exports = router;
